<template>
  <view class="t-info">
    <view class="t-b">{{ title }}</view>
    <form class="cl">
      <view class="t-a">
        <input type="number" name="phone" placeholder="请输入注册手机号" maxlength="11" v-model="phone"/>
      </view>
      <view class="t-a">
        <input type="number" name="yzm" maxlength="6" placeholder="请输入验证码" v-model="yzm"/>
        <view v-if="showText" class="t-c" @tap="getCode()">发送短信</view>
        <view v-else class="t-c">重新发送({{ second }})</view>
      </view>
      <button @tap="nextStep()">下一步</button>
    </form>
  </view>
</template>

<script>
import userApi from '@/api/user'
import passData from '@/api/requstUtil'
import {aesEncrypt, kk} from '@/common/js/crypto'

export default {
  data() {
    return {
      title: '找回密码',
      phone: '',
      yzm: '',
      second: 60,
      showText: true,
    }
  },
  methods: {
    nextStep() {
      if (this.checkFrom()) {
        let param = {
          phone: this.phone,
          smsCode: this.yzm,
          smsCodeType: "FIND_PWD"
        };
        userApi.checkSmsCode(param).then(res => {
          let data = passData(res);
          if (data) {
            let userData = {...data, phone: this.phone, smsCode: this.yzm};
            uni.navigateTo({
              url: '/pages/auth/setPassword',
              success: (res) => {
                res.eventChannel.emit('getUserData', userData)
              }
            });
          }
        });
      }


      uni.navigateTo({url: '/pages/auth/setPassword'})
    },
    checkPhoneNum() {
      if (!this.phone) {
        uni.showToast({title: '请输入手机号', icon: 'none'});
        return false;
      }
      if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(this.phone)) {
        uni.showToast({title: '请输入正确手机号', icon: 'none'});
        return false;
      }
      return true;
    },
    checkFrom() {
      if (!this.phone) {
        uni.showToast({title: '请输入手机号', icon: 'none'});
        return false;
      }
      if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(this.phone)) {
        uni.showToast({title: '请输入正确手机号', icon: 'none'});
        return false;
      }
      if (!this.yzm) {
        uni.showToast({title: '请输入验证码', icon: 'none'});
        return false;
      }
      return true;
    },
    getCode() {

      if (this.checkPhoneNum()) {

        let data = {
          phone: this.phone,
          sendSystem: "MINIP",
          smsCodeType: "FIND_PWD",
          smsCodeTemplate: '66666',
          tenantId: 'gysk'
        };
        userApi.loginSms(data).then(res => {
          this.changeCodeButton();
        })
      }
    },
    changeCodeButton() {
      var that = this;
      var interval = setInterval(() => {
        that.showText = false;
        var times = that.second - 1;
        that.second = times;
      }, 1000);
      setTimeout(() => {
        clearInterval(interval);
        that.second = 60;
        that.showText = true;
      }, 60000);
    },
  }


}
</script>

<style scoped>
.t-b {
  font-size: 18px;
  color: #000;
  padding: 35px 0 30px 0;
  font-weight: bold;
  text-align: center;
}


.t-info {
  width: 600rpx;
  margin: 0 auto;
  font-size: 28rpx;
  color: #000;
}

.t-info button {
  font-size: 14px;
  background: rgb(240, 95, 93);
  color: #fff;
  height: 42px;
  line-height: 45px;
  border-radius: 9px;
  box-shadow: 0 5px 7px 0 rgba(86, 119, 252, 0.2);
  width: 55%;
}

.t-info input {
  padding: 0 10px 0 21px;
  height: 46px;
  line-height: 46px;
  margin-bottom: 26px;
  background: #f8f7fc;
  border-bottom: 1px solid #e9e9e9;
  font-size: 15px;
  border-radius: 5px;
}

.t-info .t-a {
  position: relative;
}

.t-info .t-a image {
  width: 40rpx;
  height: 40rpx;
  position: absolute;
  left: 40rpx;
  top: 28rpx;
  border-right: 2rpx solid #dedede;
  padding-right: 20rpx;
}

.t-info .t-c {
  position: absolute;
  right: 11px;
  top: 11px;
  color: rgb(240, 95, 93);
  font-size: 12px;
  border-radius: 26px;
  height: 26px;
  line-height: 26px;
  padding: 0 13px;
  border: 1px solid rgb(240, 95, 93);
}
</style>
